package per.sunmes.lesrb.manager;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.SnapshotArray;

import per.sunmes.lesrb.action.CallAction;
import per.sunmes.lesrb.layer.Layer;
import per.sunmes.lesrb.layer.LayerEventMsg;
import per.sunmes.lesrb.util.U;

public class LayerM {
	private static LayerM _i;

	private static LayerM I() {
		if (_i == null) {
			_i = new LayerM();
		}
		return _i;
	}

	public static void dispose() {
		if (_i != null) {
			_i._dispose();
			_i = null;
		}
	}

	public static Layer addLayer(Layer layer) {
		return I()._addLayer(layer);
	}

	public static void removeDisposeLayer(Layer layer) {
		I()._removeDisposeLayer(layer);
	}

	public static Layer removeLayer(Layer layer) {
		return I()._removeLayer(layer);
	}

	public static SnapshotArray<Layer> layers() {
		return I().layers;
	}

	public static Group layerMActor() {
		return I().layerMActor;
	}

	public static InputListener layerMInputListener() {
		return I().layerMInputListener;
	}

	public static void clearCurrentAndTransLayerTo(Layer layer) {
		I()._clearCurrentAndTransLayerTo(layer);
	}

	public static void disposeLayers() {
		I()._disposeLayers();
	}

	public static void showAndInitLayer(Layer layer) {
		I()._showAndInitLayer(layer);
	}

	public static void sendEventMsgToAllLayer(LayerEventMsg msg) {
		I()._sendEventMsgToAllLayer(msg);
	}

	public static Layer getLastLayer() {
		return I()._getLastLayer();
	}

	/////////////
	SnapshotArray<Layer> layers = new SnapshotArray<Layer>();
	Group layerMActor;
	InputListener layerMInputListener = new InputListener() {
		public boolean keyDown(InputEvent event, int keycode) {
			if (event.getKeyCode() == Keys.BACK || event.getKeyCode() == Keys.ESCAPE) {
				for (int i = layers.size - 1; i >= 0; i--) {
					if (!layers.get(i).backCall()) {
						return true;
					}
				}
			}
			return false;
		};
	};

	private LayerM() {
		layerMActor = new Group();
		layerMActor.setTransform(false);
	}

	public void _clearCurrentAndTransLayerTo(final Layer layer) {
		Image mask = U.createMask();
		mask.setColor(Color.BLACK);
		mask.getColor().a = 0;
		layerMActor.addActor(mask);

		mask.addAction(Actions.sequence(Actions.fadeIn(0.2f), new CallAction() {
			public void call() {
				_disposeLayers();
				_showAndInitLayer(layer);

				Image mask = U.createMask();
				mask.setColor(Color.BLACK);
				layerMActor.addActor(mask);
				mask.addAction(Actions.sequence(Actions.fadeOut(0.2f), Actions.removeActor()));
			}
		}, Actions.removeActor()));
	}

	public void _disposeLayers() {
		while (layers.size > 0) {
			_removeDisposeLayer(layers.first());
		}
	}

	public Layer _showAndInitLayer(Layer layer) {
		_addLayer(layer);
		layer.initContent();
		return layer;
	}

	public Layer _addLayer(Layer layer) {
		if (!layers.contains(layer, true)) {
			layers.add(layer);
			layerMActor.addActor(layer);
		}
		return layer;
	}

	public void _sendEventMsgToAllLayer(LayerEventMsg msg) {
		for (Layer layer : layers) {
			layer.reciveEventMsg(msg);
		}
	}

	public Layer _getLastLayer() {
		if (layers.size == 0) {
			return null;
		}
		return layers.get(layers.size - 1);
	}

	public Layer _removeLayer(Layer layer) {
		layers.removeValue(layer, true);
		layerMActor.removeActor(layer);
		return layer;
	}

	public void _removeDisposeLayer(Layer layer) {
		layers.removeValue(layer, true);
		layerMActor.removeActor(layer);
		layer.dispose();
	}

	private void _dispose() {
		_disposeLayers();
	}
}
